<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从已存储的表示中创建 PHP 的值</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.strval.html">strval</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.unset.html">unset</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.unserialize" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">unserialize</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">unserialize</span> &mdash; <span class="dc-title">
     从已存储的表示中创建 PHP 的值
    </span></p>

   </div>
   
 <div class="refsect1 description" id="refsect1-function.unserialize-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>unserialize</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>
   )</div>

    <p class="simpara">
     <span class="function"><strong>unserialize()</strong></span> 对单一的已序列化的变量进行操作，将其转换回 
     PHP 的值。
    </p>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.unserialize-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">str</code></em></dt>

     <dd>

      <p class="para">
       序列化后的字符串。
      </p>
      <p class="para">
       若被解序列化的变量是一个对象，在成功地重新构造对象之后，PHP
     会自动地试图去调用 <a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a>
     成员函数（如果存在的话）。
      </p>
      <p class="para">
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <strong>unserialize_callback_func 指令</strong><br />
        <p class="para">
        如果在解序列化的时候需要实例化一个未定义类，则可以设置回调函数以供调用（以免得到的是不完整的
      <span class="type"><a href="language.types.object.html" class="type object">object</a></span> “__PHP_Incomplete_Class”）。可通过
      <var class="filename">php.ini</var>、<span class="function"><a href="function.ini-set.html" class="function">ini_set()</a></span> 或 <var class="filename">.htaccess</var> 
      定义‘unserialize_callback_func’。每次实例化一个未定义类时它都会被调用。若要禁止这个特性，只需置空此设定。
        </p>
       </p></blockquote>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.unserialize-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回的是转换之后的值，可为 
     <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>、<span class="type"><a href="language.types.float.html" class="type float">float</a></span>、<span class="type"><a href="language.types.string.html" class="type string">string</a></span>、<span class="type"><a href="language.types.array.html" class="type array">array</a></span>
     或 <span class="type"><a href="language.types.object.html" class="type object">object</a></span>。
     </p>
  <p class="para">
   如果传递的字符串不可解序列化，则返回 <strong><code>FALSE</code></strong>，并产生一个 <strong><code>E_NOTICE</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.unserialize-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>4.2.0</td>
       <td>
        添加了 unserialize_callback_func 指令。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.unserialize-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5160">
    <p><strong>Example #1 <span class="function"><strong>unserialize()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;这里，我们使用&nbsp;unserialize()&nbsp;装载来自数据库的&nbsp;$session_data&nbsp;数组中的会话数据。<br />//&nbsp;此例是描述&nbsp;serialize()&nbsp;的那个例子的补充。<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">odbc_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"webdb"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"php"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"chicken"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">odbc_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;data&nbsp;FROM&nbsp;sessions&nbsp;WHERE&nbsp;id&nbsp;=&nbsp;?"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sqldata&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'PHP_AUTH_USER'</span><span style="color: #007700">]);<br />if&nbsp;(!</span><span style="color: #0000BB">odbc_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sqldata</span><span style="color: #007700">)&nbsp;||&nbsp;!</span><span style="color: #0000BB">odbc_fetch_into</span><span style="color: #007700">(</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$tmp</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;如果执行出错或返回错误，则初始化为空数组<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session_data&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;现在我们需要的是&nbsp;$tmp[0]&nbsp;中已序列化的数据。<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session_data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">$tmp</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">is_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$session_data</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;出错，初始化为空数组<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$session_data&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5161">
    <p><strong>Example #2 unserialize_callback_func 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serialized_object</span><span style="color: #007700">=</span><span style="color: #DD0000">'O:1:"a":1:{s:5:"value";s:3:"100";}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;unserialize_callback_func&nbsp;从&nbsp;PHP&nbsp;4.2.0&nbsp;起可用<br /></span><span style="color: #0000BB">ini_set</span><span style="color: #007700">(</span><span style="color: #DD0000">'unserialize_callback_func'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'mycallback'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;设置您的回调函数<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">mycallback</span><span style="color: #007700">(</span><span style="color: #0000BB">$classname</span><span style="color: #007700">)&nbsp;<br />{<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;只需包含含有类定义的文件<br />&nbsp;&nbsp;&nbsp;//&nbsp;$classname&nbsp;指出需要的是哪一个类<br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.unserialize-notes">
  <h3 class="title">注释</h3>
  <div class="warning"><strong class="warning">Warning</strong>
   <p class="para">
    如果反序列化了 <strong><code>FALSE</code></strong> 的值，或者在过程中发生了错误，都会返回 <strong><code>FALSE</code></strong>。
    可以通过 <em><code class="parameter">str</code></em> 和 <em>serialize(false)</em> 进行比较，或者捕捉 <strong><code>E_NOTICE</code></strong> 错误来判断这种特殊情况。
   </p>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.unserialize-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.serialize.html" class="function" rel="rdfs-seeAlso">serialize()</a> - 产生一个可存储的值的表示</span></li>
    <li class="member"><a href="language.oop5.autoload.html" class="link">自动加载对象</a></li>
    <li class="member"><a href="var.configuration.html#unserialize-callback-func" class="link">unserialize_callback_func</a></li>
    <li class="member"><a href="language.oop5.magic.html#object.wakeup" class="link">__wakeup()</a></li>
   </ul>
  </p>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.strval.html">strval</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.unset.html">unset</a></div>
 <div class="up"><a href="ref.var.html">Variable handling 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
